var express = require('express');
var  router = express.Router();
var query = express.Router();
var  IP = require('../models/data');

IP = new IP();

module.exports = function (app) {
  app.use('/', router);
  app.use('/q', query);
};

router.get('/', function (req, res, next) {
    var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
    var data = IP.lookup(ip);

    res.render('index', {
        ip : ip,
        inf : data._des,
        network : data._as,
        lat : data._lat,
        lon : data._lon
    });
});

router.post('/', function(req, res, next) {
    var ip = req.body.ip;
    var data = IP.lookup(ip);

    res.render('index', {
        ip : ip,
        inf : data._des,
        network : data._as,
        lat : data._lat,
        lon : data._lon
    });
});

query.get('/', function (req, res) {
    res.json(IP.lookup(req.query["ip"]));
})